//
//  DLPhotoSelectorController.swift
//  图片选择器
//
//  Created by danli on 15/12/30.
//  Copyright © 2015年 danli. All rights reserved.
//

import UIKit

class DLPhotoSelectorController: UICollectionViewController {

    private var photos:[UIImage] = [UIImage]()
    private let maxPhotoCount = 6
    private var selectedIndexPath:NSIndexPath?
    
    private let layout = UICollectionViewFlowLayout()
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView?.backgroundColor = UIColor.whiteColor()
        prepareCollectionView()
    }
    
    ///cell重用标识
    private let reuserIdentify = "DLPhotoCollectionCell"
    func prepareCollectionView()
    {
        layout.itemSize = CGSize(width: 80, height: 80)
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        
        // 注册cell
        collectionView?.registerClass(DLPhotoCollectionCell.self, forCellWithReuseIdentifier: reuserIdentify)
    }
    
    // MARK: 实现collectionView的代理方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return photos.count < maxPhotoCount ? photos.count + 1 : maxPhotoCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuserIdentify, forIndexPath: indexPath) as! DLPhotoCollectionCell
        cell.delegate = self
        
        if indexPath.item < photos.count
        {
            cell.image = photos[indexPath.item]
        }
        else
        {
            cell.addAddBtn()
        }
        
        return cell
    }

}

extension DLPhotoSelectorController: DLPhotoCollectionCellDelegate, UIImagePickerControllerDelegate,UINavigationControllerDelegate
{
    /// 添加按钮点击事件
    func photoCollectionCellAddBtnDidClick(cell:DLPhotoCollectionCell) {
        // 判断系统的相册是否可用
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)
        {
            print("系统相册不可用")
            return
        }
        
        // 获取系统图片控制器
        let photoVC = UIImagePickerController()
        photoVC.delegate = self
        presentViewController(photoVC, animated: true, completion: nil)
        
        // 获取当前选cell的indexPath
        selectedIndexPath = collectionView?.indexPathForCell(cell)
    }
    
    /// 删除按钮点击事件
    func photoCollectionCellRemoveBtnDidClick(cell:DLPhotoCollectionCell) {
        selectedIndexPath = collectionView?.indexPathForCell(cell)

        photos.removeAtIndex(selectedIndexPath!.item)
        collectionView?.reloadData()
    }
    
    // MARK: 实现UIImagePickerControllerDelegate代理方法
    // 当实现了这个方法，需要手动关闭控制器
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        
        // 系统图片太大，消耗内存和流量， 要缩小图片
        let newImage = image.reduceImage()
        print(newImage)
        
        picker.dismissViewControllerAnimated(true, completion: nil)
        if selectedIndexPath?.item < photos.count
        {
            photos[selectedIndexPath!.item] = newImage
        }
        else
        {
            photos.append(newImage)
        }
        collectionView?.reloadData()
    }
}
